﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Topro.Admin.Controllers
{
    using System.Text.RegularExpressions;
    using Topro.Application.Interface;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.Info;
    using Topro.Entity.Response.Info;
    using Topro.Util;

    /// <summary>
    /// 消息控制器
    /// </summary>
    public class InfoController : Controller
    {
        #region 注入
        /// <summary>
        /// 消息接口
        /// </summary>
        private readonly InfoService _infoService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="authService">消息接口</param>
        public InfoController(InfoService infoService)
        {
            _infoService = infoService;
        }
        #endregion

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AuthFilter(AuthCode.Info0001)]
        public IActionResult Index()
        {
            ViewBag.infoType = EnumUtil.ToDictionary(typeof(InfoType));

            return View();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<PermissionResponse></returns>
        [AuthFilter(AuthCode.Info0001)]
        public IActionResult InfoQuery(InfoQuery query)
        {
            var list = _infoService.GetPager(query);

            return PartialView("_InfoQuery", list);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.Info0002, AuthCode.Info0003)]
        public IActionResult AddOrEdit(int? id)
        {
            var model = new InfoResponse();
            if (id.HasValue)
            {
                model = _infoService.GetInfo(id.Value);
            }

            ViewBag.infoType = EnumUtil.ToDictionary(typeof(InfoType));

            return View("Edit", model);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.Info0002, AuthCode.Info0003)]
        public JsonResult Save(SaveInfoRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                #region 验证
                //班次
                if (!request.Type.HasValue)
                {
                    throw new Exception("请选择类型.");
                }
                //值
                if (string.IsNullOrEmpty(request.Value))
                {
                    throw new Exception("值不能为空.");
                }
                //内容
                if (string.IsNullOrEmpty(request.Message))
                {
                    throw new Exception("内容不能为空.");
                }
                var messageReg = new Regex(@"^.{2,64}$");
                if (!messageReg.IsMatch(request.Message))
                {
                    throw new Exception("内容必须介于 2 和 64 之间");
                }
                #endregion

                response = _infoService.Save(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        [HttpPost]
        [AuthFilter(AuthCode.Info0004)]
        public JsonResult UpdateStatus(int id, sbyte status)
        {
            var response = new JResult()
            {
                Success = false
            };

            response = _infoService.UpdateUserStatus(id, status);

            return Json(response);
        }
    }
}
